package com.imooc.distributedemo.leetcode;

/**
 * 链表反转
 */
public class RevertList {

    public static ListNode reverseList(ListNode head) {
        ListNode cur = head, pre = null;
        while (cur != null) {
            ListNode tmp = cur.next;  // 暂存后继节点  cur.next
            cur.next = pre;  // 修改next饮用指向
            pre = cur;       // pre暂存cur
            cur = tmp;       // cur访问下一个节点
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode headNode = new ListNode();
        ListNode node = new ListNode(0);
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        //创建一个链表
        headNode = node;
        node.next = node1;
        node1.next  = node2;
        node2.next = node3;
        node3.next = node4;
        System.out.println(headNode);

        ListNode reversedList = reverseList(headNode);
        System.out.println(reversedList);
    }
}

